#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def transform_metadata(metadata):
    metadata['type'] = get_type_by_name(metadata.get('name'))
    metadata['dictList'] = get_dict_by_name(metadata.get('name'))
    return metadata


dict_type_name = {
    'text_id': 180,
    'text': 1,
    'area': 101,
    'rich': 102,
    'link': 103,
    'cron': 104,
    'imgs': 111,
    'img': 110,
    'icon': 110,
    'coding': 120,
    'region': 130,
    'tags': 140,
    'params': 150,
    'specifications': 151,
    'files': 161,
    'file': 160,
    'address': 170,
    'num': 2,
    'status': 201,
    'flag': 202,
    'price': 210,
    'date': 3,
    'enum': 4,
    'dict': 410,
}


def get_type_by_name(name: str):
    for key in dict_type_name:
        if name.startswith(key):
            return dict_type_name.get(key)

    return 1


def get_dict_by_name(name):
    if not name.startswith('dict_'):
        return []
    from lj_orm import tool_dict
    dict_name = name[5:]
    return [{'name': item.name, 'val': item.id} for item in tool_dict.listByPCode(dict_name)]
